package F21AS_Freddy.AFJ_Taxikiosk.src;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.event.ChangeListener;

/**
 * KIOSK GUI used to define interface
 * @author Freddy Wong
 * F21AS Course Work Stage2 @ Year 2015
 */
public class KioskGUI extends JFrame implements Observer {
	
	private JTextArea currentKioskTextArea;
	private Kiosk kiosk;
	private JButton closeKioskButton;
	private int counter;

	public KioskGUI(Kiosk kiosk, int counter) {

		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.counter = counter;
		this.kiosk = kiosk;
		kiosk.addObserver(this);
		this.setTitle("Kiosk " + counter + " - CLOSED");

	
		Container container = getContentPane();
		container.add(createControlPanel(), BorderLayout.NORTH);

		currentKioskTextArea = new JTextArea();
		currentKioskTextArea.setEditable(false);
		currentKioskTextArea.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		currentKioskTextArea.setSize(300, 200);

		container.add(currentKioskTextArea, BorderLayout.CENTER);

		this.setSize(425, 200);
		this.setLocation(570, 10);	

	}

	/**
	 * update method called by model (Kiosk) when changes occur this
	 * method updates the GUI
	 */
	@Override
	public void update(Observable observable, Object arg1) {

		kiosk = (Kiosk) observable;
		
		closeKioskButton.setEnabled(kiosk.getIsOpen());
		Passenger passenger = kiosk.getCurrentPassenger();
		if (kiosk.getIsOpen()) {
			setTitle("KIOSK " + counter + "- OPEN");
			currentKioskTextArea.setText("");
			currentKioskTextArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));

			if (passenger != null) {

				String display = "";
				display += String.format("%-20s %-20s\n", "KIOSK:", counter);
				display += String.format("%-20s %-20s\n", "Booking Reference:", passenger.getBookingRef());
				display += String.format("%-20s %-20s\n", "Customer Name:", passenger.getBookingName());
				display += String.format("%-20s %-20s\n", "Passenger:", passenger.getNumberOfPassenger());
				display += "\n";
				passenger.assignKiosk(counter);
				
				currentKioskTextArea.setText(display);
			} else {
				//currentKioskTextArea.setText("\n		PASSENGER DROP-OFF\n\n                    KIOSK EMPTY");
				currentKioskTextArea.setText("\n		KIOSK " + counter + " NO PASSENGER");
			}
		} else {
			setTitle("KIOSK " + counter + "- CLOSED");
			currentKioskTextArea.setFont(new Font(Font.MONOSPACED, Font.BOLD, 18));
			currentKioskTextArea.setText("\n	   KIOSK " + counter + " CLOSED");
			this.add(currentKioskTextArea);
		}

	}

	/**
	 * Create KIOSK control panel 
	 * @return
	 */
	private JPanel createControlPanel() {
		closeKioskButton = new JButton("Close Desk");
		JPanel controlPanel = new JPanel();
		
		controlPanel.add(closeKioskButton);
		// initial state of the button disabled until the desk opens
		closeKioskButton.setEnabled(false);
		return controlPanel;

	}
	
	/**
	 * Set the action listener on the Close Desk button to handle click events
	 * @param al
	 */
	public void addCloseKioskButtonListener(ActionListener al) {
		closeKioskButton.addActionListener(al);
	}

	/**
	 * method to allow changes to the title of the JFrame
	 * @param title
	 * - string to set window title
	 */
	public void setFrameTitle(String title) {
		this.setTitle(title);
	}
	
} // End of KioskGUI Class
